/*
 * DataContainer.cpp
 *
 *      Author: Jerome RD Soine and Christoph A Brand
 *      Institution: Schwarz goup, Institute for Theoretical Physics, Heidelberg University, Germany
 */

#include "../../../include/base/utility/DataContainer.h"



DataContainer::DataContainer(string f)
{
	filename = f;
	read_data();
}


Displacement DataContainer::get_displacement_object(unsigned int n)
{
	return (*displacements)[n];
}

vector<double> DataContainer::get_position(unsigned int data_point)
{
	vector<double> tmp (2,0);
	tmp[0] = (*displacements)[data_point].get_P(0);
	tmp[1] = (*displacements)[data_point].get_P(1);
	return tmp;
}

vector<double> DataContainer::get_displacement(unsigned int data_point)
{
	vector<double> tmp (2,0);
	tmp[0] = (*displacements)[data_point].get_V(0);
	tmp[1] = (*displacements)[data_point].get_V(1);
	return tmp;
}

void DataContainer::read_data()
{
	displacements = new vector<Displacement>();

    std::ifstream data (filename.c_str());
    if(data.good())
    {
    dealii::Vector<double> tmp_vec (2);
    dealii::Point<2> tmp_pos;

    while(!data.eof())
    {
            for(unsigned int i=0;i<2;i++) data >> tmp_pos(i);
            for(unsigned int i=0;i<2;i++) data >> tmp_vec(i);
            displacements->push_back(Displacement (tmp_pos,tmp_vec));
    }
    data.close();

    cout << "N_points = " << (*displacements).size() << "\n";
    }
    else
    {
    	cout << "ERROR: filename " << filename << " -> no such file or directory." << endl;
    	exit(1);
    }

}

void DataContainer::write_displacments_to_file(string f)
{
	std::ofstream data (f.c_str());

	for(unsigned int i=0;i<get_n_data_points();i++)
	{
		data << ((*displacements)[i].get_point())(0) << "\t" <<	((*displacements)[i].get_point())(1) << "\n";
	}

	data.close();
}
